package cn.com.sixboy.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.com.sixboy.entity.Shanghu;
import cn.com.sixboy.service.common.ShanghuAdminService;
import cn.com.sixboy.utils.DateTimeUtil;

@Controller
@RequestMapping("/admin/shanghu")
public class ShanghuAdminController {

	private final static Logger log = Logger
			.getLogger(ShanghuAdminController.class);

	@Autowired
	private ShanghuAdminService shanghuAdminService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	public String list(Model model) {
		List<cn.com.sixboy.entity.Shanghu> list = shanghuAdminService
				.findAllShanghu(); // 查询所有学生对象
		model.addAttribute("list", list); // 赋值给属性stus，传值到jsp页面中，可用jstl在页面输出详细值
		return "admin/shanghu/list"; // 返回到指定到视图(indexstu.jsp)
	}

	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "admin/shanghu/add"; // 返回到指定到视图(indexstu.jsp)
	}

	@RequestMapping(value = "/add")
	public String add(Model model, cn.com.sixboy.entity.Shanghu obj,
			@RequestParam MultipartFile[] logoFiles, HttpServletRequest request) {
		// 获取路径
		String ctxPath = request.getSession().getServletContext()
				.getRealPath("/")
				+ "upload/shanghu/";
		// 创建文件
		File dirPath = new File(ctxPath);
		if (!dirPath.exists()) {
			dirPath.mkdir();
		}

		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < logoFiles.length; i++) {
			if (logoFiles[i] != null && logoFiles[i].getSize() > 0) {
				String filename = logoFiles[i].getOriginalFilename();
				String imgtype = filename.substring(filename.lastIndexOf("."));
				String newFilePath = DateTimeUtil.getDateTime17String()
						+ imgtype;
				File uploadFile = new File(ctxPath + newFilePath);
				try {
					FileCopyUtils.copy(logoFiles[i].getBytes(), uploadFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
				buf.append(newFilePath).append(",");
			}
		}
		if (buf.length() > 0) {
			buf.deleteCharAt(buf.length() - 1);
			obj.setLogos(buf.toString());
		} else {

		}
		log.debug(obj);
		// 调用业务层入库操作
		shanghuAdminService.addShanghu(obj);

		return list(model); // 返回到指定到视图(indexstu.jsp)
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delete")
	public String delete(Model model, cn.com.sixboy.entity.Shanghu obj) {
		log.debug(obj);
		// 调用业务层入库操作
		shanghuAdminService.delete(obj);

		return list(model); // 返回到指定到视图(indexstu.jsp)
	}

	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Model model, cn.com.sixboy.entity.Shanghu obj) {
		obj = (Shanghu) shanghuAdminService.querySingleEntity(Shanghu.class,
				obj.getId());
		model.addAttribute("obj", obj);
		return "admin/shanghu/update"; // 返回到指定到视图(indexstu.jsp)
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/update")
	public String update(Model model, cn.com.sixboy.entity.Shanghu obj,
			@RequestParam MultipartFile[] logoFiles, HttpServletRequest request) {

		Shanghu oldobj = (Shanghu) shanghuAdminService.querySingleEntity(
				Shanghu.class, obj.getId());
		// 获取路径
		String ctxPath = request.getSession().getServletContext()
				.getRealPath("/")
				+ "upload/shanghu/";
		// 创建文件
		File dirPath = new File(ctxPath);
		if (!dirPath.exists()) {
			dirPath.mkdir();
		}

		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < logoFiles.length; i++) {
			if (logoFiles[i] != null && logoFiles[i].getSize() > 0) {
				String filename = logoFiles[i].getOriginalFilename();
				String imgtype = filename.substring(filename.lastIndexOf("."));
				String newFilePath = DateTimeUtil.getDateTime17String()
						+ imgtype;
				File uploadFile = new File(ctxPath + newFilePath);
				try {
					FileCopyUtils.copy(logoFiles[i].getBytes(), uploadFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
				buf.append(newFilePath).append(",");
			}
		}
		if (buf.length() > 0) {
			buf.deleteCharAt(buf.length() - 1);
			obj.setLogos(buf.toString());
		} else {
			obj.setLogos(oldobj.getLogos());
		}
		log.debug(obj);
		// 调用业务层入库操作
		shanghuAdminService.update(obj);

		return list(model); // 返回到指定到视图(indexstu.jsp)
	}
}
